//
//  ViewController.m
//  CollPushAnimation
//
//  Created by jiangjunwy on 16/7/28.
//  Copyright © 2016年 SanYouApp. All rights reserved.
//

#import "ViewController.h"
#import "mgyCell.h"
#import "secondViewController.h"

//设备宽高
#define kDeviceWidth [UIScreen mainScreen].bounds.size.width
#define KDeviceHeight [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UIViewControllerTransitioningDelegate>{
    UICollectionView * mainCollectionView ;
    NSArray * picArr;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initHeadDateView];
    picArr = @[@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"7.jpg",@"8.jpg",@"9.jpg",@"10.jpg",@"11.jpg",@"12.jpg",@"13.jpg",@"14.jpg",@"15.jpg",@"16.jpg",@"17.jpg",@"18.jpg",@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"7.jpg",@"8.jpg",@"9.jpg",@"10.jpg",@"11.jpg",@"12.jpg",@"13.jpg",@"14.jpg",@"15.jpg",@"16.jpg",@"17.jpg",@"18.jpg"];
    // Do any additional setup after loading the view, typically from a nib.
}


-(void)initHeadDateView{
    
    
    UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing = 5.0;//行间距(最小值)
    flowLayout.minimumInteritemSpacing = 0.0;//item间距(最小值)
    flowLayout.itemSize = CGSizeMake(kDeviceWidth, 200);//item的大小
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);//设置section的边距
//    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, KDeviceHeight) collectionViewLayout:flowLayout];
    mainCollectionView.showsHorizontalScrollIndicator = NO ;
    mainCollectionView.bounces       = YES ;
    mainCollectionView.dataSource    = self;
    mainCollectionView.delegate      = self;
    mainCollectionView.backgroundColor = [UIColor colorWithWhite:0.953 alpha:1.000];
    [mainCollectionView registerNib:[UINib nibWithNibName:@"mgyCell" bundle:nil] forCellWithReuseIdentifier:@"mgyCell"];
    [self.view addSubview:mainCollectionView];
}


#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return picArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    mgyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"mgyCell" forIndexPath:indexPath];
    cell.picimageview.image = [UIImage imageNamed:[picArr objectAtIndex:indexPath.row]];
    
    return cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView
                                                           cellForItemAtIndexPath:indexPath];
    CGRect rectInSuperview = [collectionView convertRect:cell.frame toView:[collectionView superview]];
    self.picimage = [[UIImageView alloc]initWithFrame:rectInSuperview];
    self.picimage.image = [UIImage imageNamed:[picArr objectAtIndex:indexPath.row]];
    NSLog(@"%ld %ld ", (long)indexPath.section, (long)indexPath.row);
    
    secondViewController* secondVC = [[secondViewController alloc] initWithNibName:nil bundle:nil];
    secondVC.transitioningDelegate = self;
    secondVC.titImage = [UIImage imageNamed:[picArr objectAtIndex:indexPath.row]];
    [self presentViewController:secondVC animated:YES completion:nil];
    
    [self performSelector:@selector(startPresentAnimation) withObject:nil afterDelay:.2];
}

- (void)startPresentAnimation
{
    [self.login startAnimation];
}

- (MGYTranslation *)login
{
    if (!_login) {
        _login = [[MGYTranslation alloc] initWithView:self.picimage];
    }else{
        _login.animationView = self.picimage;
    }
    return _login;
}

#pragma mark UIViewControllerTransitioningDelegate
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    self.login.reverse = YES;
    return self.login;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    self.login.reverse = NO;
    return self.login;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
